package com.prs.crm.dao.sys.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.sys.SysDepartmentDao;
import com.prs.crm.domain.sys.SysDepartment;

public class SysDepartmentDaoImpl extends BaseDaoImpl<SysDepartment> implements SysDepartmentDao{

	@SuppressWarnings("unchecked")
	@Override
	public Collection<SysDepartment> getAllRootSysDepartment() {
		Criteria criteria=this.getSession().createCriteria(SysDepartment.class);
		criteria.add(Restrictions.isNull("parent"));
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysDepartment> getAllSysDepartment() {
		Criteria criteria=this.getSession().createCriteria(SysDepartment.class);
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<SysDepartment> getRootSysDepartment() {
		Criteria criteria=this.getSession().createCriteria(SysDepartment.class);
		criteria.add(Restrictions.isNull("parent"));
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}

	@Override
	public SysDepartment getSysDepartment(final String code) {
		Criteria criteria=this.getSession().createCriteria(SysDepartment.class);
		criteria.add(Restrictions.isNull("parent"));
		criteria.add(Restrictions.eq("companyCode", code));
		List result=criteria.list();
		if(result==null||result.size()==0){
			return null;
		}
		return (SysDepartment) criteria.list().get(0);
	}

}
